package com.ossdemo.controller;

import com.aliyun.oss.model.OSSObjectSummary;
import com.ossdemo.model.vo.Result;
import com.ossdemo.strategy.UploadStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

@RestController
public class uploadController {

    @Autowired
    private UploadStrategy uploadFile;

    /**
     * 返回上传图片的url
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadPicture")
    public Result<String> uploadPicture(@RequestParam("file") MultipartFile file) {
        // 头尾的斜杆必须有
        String path = "/photo/";
        // 第二个参数对应bucketName下的文件夹名称
        String url = uploadFile.uploadFile(file, path);
        return Result.success(url);
    }

    /**
     * 获取photo文件下的所有图片
     *
     * @return 返回数组（图片的访问地址）
     */
    @GetMapping("/getAllPicture")
    public Result<List<String>> getAllPicture() {
        // 要文件名即可
        String path = "photo";
        List<String> photo = uploadFile.getPhoto(path);
        return Result.success(photo);
    }
    @PostMapping("/deletePicture")
    public Result<String> deletePicture(@RequestBody() String path) {
        System.out.println("================================删除文件成功====" + path);
        String path1 = path.replaceAll("\"", "");
        uploadFile.deletePictureByUrl(path1);
        return Result.success();
    }
}
